package CodonCheck;

use strict;
use warnings;
use Bio::Seq;
use Bio::SeqIO;

sub new {
	my ($class) = @_;

	my $self = ();
	$self->{file} = undef;
	$self->{seqs} = undef;
	$self->{data} = undef;
	$self->{good} = undef;
	$self->{goodt} = 0;
	$self->{total} = 0;
	bless $self, $class;
	return $self;
}

sub Load {
	my ($self, $filename) = @_;
	$self->{file} = $filename;

	my $seqs = Bio::SeqIO->new(-format=>'fasta',-file=>"$filename");
	$self->{seqs} = $seqs;
}

sub Check {
	my ($self, $table) = @_;
	my $seqs = $self->{seqs};
	my @data = ();
	my @good = ();

	my $count = 0;
	my $goodt = 0;

L:	while(my $seq = $seqs->next_seq){
		#print "\$seq: " . $seq->id . "\n";
		$count++;
		for(my $o = 0;$o < 3; $o++){
			my $seqtr = $seq->translate(undef,undef,$o,$table);
			my $trans = $seqtr->seq();

			my @sp = split /\*/, $trans;
			if(length($sp[0])/length($trans) > .9){	
				push @good, $seq->id;
				$goodt++;
				next L;
			}
		}		
		push @data,$seq->id;	
		#print $seq->id . " has too many stop codons\n";
	}
	$self->{data} = [ @data ];
	$self->{good} = [ @good ];
	$self->{total} = $count;
	$self->{goodt} = $goodt;
	return @data;
}

sub GetCorrect {
	my $self = shift;
	return $self->{goodt};
}

sub GetTotal {
	my $self = shift;
	return $self->{total};
}

sub GetAnalysis {
	my $self = shift;
	my $data = $self->{data};
	return @$data;
}

sub GetGood {
	my $self = shift;
	my $data = $self->{good};
	return @$data;
}

1;
